package homework.james.xiao.bean;

import homework.james.xiao.util.Util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
 * 若干天的orders的集合
 * @author eyngxao
 * @version $Revision: $
 */
@XStreamAlias("ordersHistory") 
public class OrdersHistory {
	private static OrdersHistory oh=null;
	private ArrayList<Orders> ordersHistory = new ArrayList<Orders>() ;
	private static final String DIR = "/src/homework/james/xiao/xml/";
	private static final String FILENAME ="ordersHistory.xml";
	
	public static OrdersHistory getInstance(){
		if (oh == null){
			return initializeOrderHistory();
		}else{
			return oh;
		}
	}
	private OrdersHistory(){
		
	}
	private static OrdersHistory initializeOrderHistory() {
		XStream xstream = new XStream(new DomDriver());
		Util.alias(xstream);
		try {
			oh = (OrdersHistory)xstream.fromXML(new FileInputStream(System.getProperty("user.dir")+ DIR+ FILENAME));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return oh;
	}
	
	public void writeToXML(){
		
		//oh.addOrders(o2);
		
		XStream xstream = new XStream(new DomDriver());
		Util.alias(xstream);
		
		FileOutputStream fop=null;
		try {
			fop = new FileOutputStream(System.getProperty("user.dir")+ DIR+ FILENAME);
			fop.write(xstream.toXML(oh).getBytes("utf-8"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fop.close();
			} catch (IOException e){
				e.printStackTrace();
			}
		}
		
		
	}
	public void print(PrintWriter ps){
		
		for(int i=0;i<ordersHistory.size();i++){
			if(i==0)
				ordersHistory.get(i).print(ps,true, false);
			else if (i+1==ordersHistory.size())
				ordersHistory.get(i).print(ps,false,true);
			else
				ordersHistory.get(i).print(ps,false,false);
		}
	}
	public static void main(String[] args){
		Orders o1 = new Orders();
		
		o1.addOrder(new Order(new User("James"),new Dish("盖浇饭","10")));
		o1.addOrder(new Order(new User("Fred"),new Dish("拉肠","7")));
		
		
		
		o1.addOrder(new Order(new User("Profee"),new Dish("蜜汁叉烧饭","12")));
		o1.addOrder(new Order(new User("Astro"),new Dish("牛柳蛋铺饭","15")));
		oh = new OrdersHistory();
		oh.addOrders(o1);
		oh.writeToXML();
		//initializeOrderHistory();
		//oh.print(System.out);
	}
	public ArrayList<Orders> getOrdersHistory() {
		return ordersHistory;
	}
	public void setOrdersHistory(ArrayList<Orders> ordersHistory) {
		this.ordersHistory = ordersHistory;
	}
	public void addOrders(Orders orders){
		ordersHistory.add(orders);
	}
	
}
